#!/bin/bash

echo "🧪 全面测试 cbp2make_ai AI 功能"
echo "===================================="

if [ ! -f "bin/cbp2make_ai" ]; then
    echo "❌ 可执行文件不存在"
    exit 1
fi

echo "1. 测试 AI 帮助信息"
echo "-------------------"
./bin/cbp2make_ai --ai-help

echo ""
echo "2. 测试项目分析功能"
echo "-------------------"
# 创建一个测试项目文件
cat > test_analyze.cbp << 'TESTANALYZE'
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
    <Option title="AI Test Project" />
    <Build>
        <Target title="Debug">
            <Option output="bin/debug/ai_test" />
            <Option type="1" />
            <Compiler>
                <Add option="-g" />
                <Add option="-O0" />
            </Compiler>
        </Target>
    </Build>
    <Unit filename="main.cpp" />
    <Unit filename="utils.cpp" />
    <Unit filename="ai_module.cpp" />
</Project>
</CodeBlocks_project_file>
TESTANALYZE

echo "分析测试项目:"
./bin/cbp2make_ai --ai-analyze test_analyze.cbp

echo ""
echo "3. 测试 Makefile 优化"
echo "--------------------"
# 创建一个简单的 Makefile 用于测试
cat > test_makefile << 'TESTMAKEFILE'
# 测试 Makefile
CXX = g++
CXXFLAGS = -Wall -std=c++11
TARGET = myapp
SOURCES = main.cpp utils.cpp

$(TARGET): $(SOURCES)
	$(CXX) $(CXXFLAGS) -o $(TARGET) $(SOURCES)

clean:
	rm -f $(TARGET)
TESTMAKEFILE

echo "优化测试 Makefile:"
./bin/cbp2make_ai --ai-optimize test_makefile

echo ""
echo "4. 测试代码注释生成"
echo "------------------"
# 创建测试源文件
mkdir -p test_src
cat > test_src/main.cpp << 'TESTCPP'
#include <iostream>
#include <vector>

class TestClass {
private:
    int value;
public:
    TestClass(int v) : value(v) {}
    void print() {
        std::cout << "Value: " << value << std::endl;
    }
};

int main() {
    TestClass obj(42);
    obj.print();
    return 0;
}
TESTCPP

echo "为测试文件生成注释:"
./bin/cbp2make_ai --ai-comment test_src/main.cpp

if [ -f "test_src/main.cpp.ai_comments.txt" ]; then
    echo "✅ 注释文件已生成"
    echo "注释文件内容:"
    cat test_src/main.cpp.ai_comments.txt
else
    echo "❌ 注释文件未生成"
fi

echo ""
echo "5. 测试交互模式"
echo "---------------"
echo "测试交互模式 (自动输入 quit 退出):"
echo -e "help\\nanalyze test.cbp\\nquit" | timeout 5s ./bin/cbp2make_ai --ai-interactive 2>&1 | head -20

echo ""
echo "6. 验证原有功能仍然正常"
echo "----------------------"
./bin/cbp2make_ai --version
echo "退出码: $?"

# 清理测试文件
rm -f test_analyze.cbp test_makefile
rm -rf test_src

echo ""
echo "===================================="
echo "测试完成!"
